package com.mac.intellect_flow.admin.controller.system;

import com.mac.intellect_flow.framework.model.LoginUser;
import com.mac.intellect_flow.framework.util.SecurityUtils;
import com.mac.intellect_flow.reply.R;
import com.mac.intellect_flow.sys.entity.SysMenu;
import com.mac.intellect_flow.sys.model.LoginBody;
import com.mac.intellect_flow.sys.service.SysLoginService;
import com.mac.intellect_flow.sys.service.SysMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 登录验证
 * 
 * @author mac
 */
@Tag(name = "登录管理")
@RestController
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private SysMenuService menuService;
/*
    @Autowired
    private CurPermissionService permissionService;*/

    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @Operation(summary = "登录")
    @PostMapping("/login")
    public R login(@RequestBody LoginBody loginBody) {
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        return R.success(token);
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @Operation(summary = "获取用户信息")
    @GetMapping("getInfo")
    public R getInfo() {
        LoginUser user = SecurityUtils.getLoginUser();
        Map<String,Object> map = new HashMap<>();
        map.put("user", user);
        map.put("roles", user.getRoles());
        map.put("permissions", user.getPermissions());
        return R.success(map);
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @Operation(summary = "获取路由信息")
    @GetMapping("getRouters")
    public R getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return R.success(menuService.buildMenus(menus));
    }
}
